﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using BackOffice.Context.UltiPro;

namespace BackOffice.Automation.Payroll.Extensions
{
    public static class TestEnvironment
    {
        internal static Dictionary<string, string> GetEnvironmentServersFromXML()
        {
            Dictionary<string, string> servers = new Dictionary<string, string>();
            List<string> serverTypes = new List<string>() { "SqlServer", "SiteServer", "WebServer" };

            XDocument doc = XDocument.Load(EnvironmentXmlFile);

            List<XElement> nodesList = doc.Root.Elements().ToList<XElement>();

            XElement node;
            List<XElement> serverNodes = new List<XElement>();
            for (int i = 0; i < nodesList.Count; i++)
            {
                node = nodesList[i];

                if (node.Attribute("Name").Value.Equals(EnvironmentName))
                {
                    serverNodes = node.Descendants().ToList<XElement>();
                    break;
                }
            }

            for (int i = 0; i < serverNodes.Count; i++)
            {
                node = serverNodes[i];
                string nodeName = node.Name.LocalName;
                string serverName = node.Attribute("Name") == null ? "" : node.Attribute("Name").Value;

                if (serverTypes.Contains(nodeName))
                {
                    servers.Add(nodeName, serverName);
                }
            }

            return servers;
        }

        internal static string EnvironmentName
        {
            get
            {
                return Helpers.GetConfigValue("EnvironmentName");
            }
        }

        internal static string EnvironmentXmlFile
        {
            get
            {
                return Helpers.GetConfigValue("EnvironmentsXML");
            }
        }
        
        public static string SqlServer
        {
            get
            {
                return GetEnvironmentServersFromXML()["SqlServer"];
            }
        }

        public static string SiteServer
        {
            get
            {
                return GetEnvironmentServersFromXML()["SiteServer"];
            }

        }

        public static string WebServer
        {
            get
            {
                return GetEnvironmentServersFromXML()["WebServer"];
            }
        }
    }

}
